package catalogue;

import java.sql.*;

public class QueryBD {

    Catalogue cataDVD;
    Connection cx;

    public QueryBD() {
        try {
            //Class.forName("oracle.jdbc.driver.OracleDriver");
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
            System.exit(1);
        }
        try {
            //String url = "jdbc:oracle:thin:@miage03.dmiage.u-paris10.fr:1521:MIAGE";
            String url = "jdbc:mysql://localhost:3306/catalogue";
            //cx = DriverManager.getConnection(url, "aspasquet", "classique2011pw");
            cx = DriverManager.getConnection(url, "root", "");
            Statement st = cx.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM catalogue");

            cataDVD = new Catalogue();

            DVD d;
            while (rs.next()) {
                d = new DVD();
                d.setiD(rs.getInt("ID"));
                d.setDescription(rs.getString("Description"));
                d.setPrix(rs.getDouble("Prix"));
                cataDVD.getCat().add(d);
            }

            rs.close();
            st.close();
            //cx.close();

        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
            System.exit(1);
        }
    }

    public Catalogue recupCatalogue() {
        return cataDVD;
    }

    public void ajouteDVD(String description, double prix) {
        try {
            PreparedStatement pst = cx.prepareStatement("INSERT INTO catalogue(ID,Description,Prix) VALUES (?,?,?)");
            int iD = cataDVD.getCat().size();
            pst.setInt(1,iD);
            pst.setString(2,description);
            pst.setDouble(3,prix);
            
            pst.executeUpdate();
        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
            System.exit(1);
        }
    }
}
